// eslint-disable-next-line
/* eslint-disable */
import axios from "axios";

import router from "@/router";

let token = "";

let urls = "";
console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV === "production") {
  //线上环境
  urls = "https://api.yipinkeji.cn/api/v1";
} else {
  //开发环境
  urls = "http://www.yipin.com/api/v1";
}

axios.defaults.headers.common["token"] = token;

axios.defaults.timeout = 600000;

//地址:端口
export function getUrls() {
  return urls;
}

// //图片上传
export function uploadFiles() {
  return "";
}
//图片上传
export function uploadFile() {
  return "";
}
// //上传文件到服务器
export function uploadFileServer(type) {
  return "file/uploadFile?folderType=" + type;
}

//请求拦截器
axios.interceptors.request.use(
  function (config) {
    let user = window.sessionStorage.getItem("token");
    if (user) {
      token = user;
    }
    // else{
    //   window.sessionStorage.removeItem("token");
    //   location.href = "/";
    // }
    config.headers.common["access-token"] = token;
    return config;
  },
  function (error) {
    console.info(error);
    return Promise.reject(error);
  }
);

//响应拦截
axios.interceptors.response.use(
  function (response) {
    if (response.data && [402, 403].includes(response.data.code)) {
      //登录过期
      window.sessionStorage.removeItem("token");
      router.push("/login");
    }
    return response;
  },
  function (error) {
    console.log(error, "error");
    return Promise.reject(error);
  }
);

/*
  封装图片上传
*/
export function upload(url, data) {
  return new Promise((resolve, reject) => {
    axios
      .post(uploadFileServer() + url, data, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then(
        (response) => {
          resolve(response.data);
        },
        (err) => {
          reject(err);
        }
      );
  });
}

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .get(urls + url, {
        params: params,
      })
      .then((response) => {
        resolve(response.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function toExcel(url, param) {
  return axios.request({
    url: urls + url, //完整路由地址
    method: "post", //请求类型
    data: param,
    procgress: true,
    responseType: "blob", //响应类型，避免乱码
  });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.post(urls + url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(urls + url, data).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
}

/**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function del(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios
      .delete(urls + url, {
        params: params,
      })
      .then((response) => {
        resolve(response.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
